//网络访问工具类
import axios from 'axios';
import router from '../router/index'
axios.defaults.timeout = 5000   //超时时间为5秒
axios.defaults.withCredentials = true   //允许跨域
//访问方式
//Content-type
axios.defaults.headers.post['Content-type'] = 'application/x-www-form-urlencoded;charset=UTF-8';
//基础url
axios.defaults.baseURL = "http://localhost:8888"

//响应拦截器
axios.interceptors.response.use(
    response => {
        //如果response里面的status是200，说明访问到接口了，否则错误
        if(response.status === 200){
            console.log(response.data);
            //异步执行的框架
            //正确的话继续执行
            return Promise.resolve(response);
        }else{
            //错误的话不往下执行
            return Promise.reject();
        }
    },
    //错误信息
    error => {
        if(error.response){
            switch (error.response.status) {
                case 401:   //未登录
                    router.replace({
                       path: '/',
                       query:{
                           redirect: router.currentRoute.fullPath
                       }
                    });
                    break;
                case 404:   //没找到
                    break;
            }
            return Promise.reject(error.response);
        }
    }
);

/**
 * 封装get方法
 * */
export function get(url, params = {}) {
    return new Promise((resolve, reject) => {
        axios.get(url, {params:params})
            .then(response => {
                resolve(response.data);
            })
            .catch(err => {
               reject(err);
            });
        });
}

/**
 * 封装post方法
 * */
export function post(url, data = {}) {
    return new Promise((resolve, reject) => {
        axios.post(url, data)
            .then(response => {
                resolve(response.data);
            })
            .catch(err => {
                reject(err);
            });
    });
}
